<template>
  <div class="app-container">
    <el-dialog :title="titles[flag]" :visible.sync="dialogVisible" width="50%" append-to-body>
      <div v-loading="loading">
        <el-form ref="form" :model="form" :disabled="flag==1">
          <el-row :gutter="10">
            <el-col :span="8">
              <el-form-item label="工序编码" prop="proCode" required>
                <div v-if="flag==1">{{ form.proCode }}</div>
                <el-input v-else v-model="form.proCode" placeholder="自动生成" disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="工序名称" prop="proName" required>
                <div v-if="flag==1">{{ form.proName }}</div>
                <el-input v-else v-model="form.proName" placeholder="请输入"/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="工序类型" prop="proType" required>
                <div v-if="flag==1">
                  <dict-tag :options="dict.type.ims_process_type" :value="form.proType"/>
                </div>
                <el-select v-else v-model="form.proType" style="width: 100%">
                  <el-option
                    v-for="item in dict.type.ims_process_type"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="8">
              <el-form-item label="工时(min)" prop="duration" required>
                <div v-if="flag==1">{{ form.duration }}</div>
                <el-input v-else type="number" v-model="form.duration" :min="0" placeholder="请输入"/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="工钱(元)" prop="price" required>
                <div v-if="flag==1">{{ form.price }}</div>
                <el-input v-else type="number" v-model="form.price" :min="0" placeholder="请输入"/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="所属部门" prop="deptId" required>
                <div v-if="flag==1">{{ form.deptName }}</div>
                <treeselect v-else class="myInput" :default-expand-level="1" v-model="form.deptId" :options="deptList"
                            placeholder="请选择" noResultsText="无数据" :disabled="flag==1"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="8">
              <el-form-item label="责任人" prop="userIdList" required>
                <div v-if="flag==1">
                  <span v-for="val in form.userNameList">{{ val }};</span>
                </div>
                <el-select v-else v-model="form.userIdList" multiple filterable style="width: 100%">
                  <el-option
                    v-for="item in userList"
                    :key="item.userId"
                    :label="item.nickName"
                    :value="item.userId">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="dialogOk" v-if="flag!=1">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


import {deptTreeSelect, listUser} from "../../../api/system/user";
import {addProduceProcess, updateProduceProcess} from "../../../api/produceMng";
import {getCommonCode} from "../../../api/imsMng";
import Treeselect from '@riophae/vue-treeselect'

export default {
  components: {Treeselect},
  dicts:['ims_process_type'],
  data() {
    return {
      flag: 0,
      titles: ['新增', '详情', '修改'],
      loading: false,
      dialogVisible: false,
      form: {
        userIdList: []
      },
      deptList: [],
      userList: [],
    }
  },
  watch: {
    'form.deptId'(e) {
      if (e) {
        this.listUSer()
      } else {
        this.$set(this.form, 'userIdList', [])
        this.userList = []
      }
    }
  },
  methods: {
    show(flag, form) {
      this.flag = flag
      this.dialogVisible = true
      this.resetForm('form')
      this.form = form
      if (!this.flag) {
        getCommonCode({orderType: 90}).then(res => {
          this.$set(this.form, 'proCode', res.data.orderNo)
        })
      }
      if(flag==0||flag==2){
        deptTreeSelect().then(res => {
          this.deptList = res.data
        })
      }
    },
    listUSer() {
      listUser({pageNum: 1, pageSize: 1000, deptId: this.form.deptId}).then(res => {
        this.userList = res.rows
      })
    },
    dialogOk() {
      this.$refs.form.validate(e => {
        if (e) {
          this.loading = true
          if (!this.flag) {
            addProduceProcess(this.form).then(res => {
              this.dialogVisible = false
              this.$emit('ok')
              this.$message.success('新增成功')
            }).finally(e => {
              this.loading = false
            })
          } else {
            updateProduceProcess(this.form).then(res => {
              this.dialogVisible = false
              this.$emit('ok')
              this.$message.success('修改成功')
            }).finally(e => {
              this.loading = false
            })
          }
        }
      })
    },
  }
}
</script>
